/**
 * Copyright 2012
 * VAN
 */
package vaidyaa.algorithms.sorting;

/**
 * @author vaidyaa
 * 
 */
public class InsertionSortService implements SortService {

	@Override
	public void sort(Integer[] array, boolean order) {
		if (array.length <= 1) {
			return;
		}
		for (int i = 1; i < array.length; i++) {
			Integer key = array[i];
			int j = i - 1;
			while (j >= 0 && !SortingCommons.compare(key, array[j], order)) {
				SortingCommons.swap(array,j,j+1);
				j--;
			}
			array[j+1] = key;
		}
	}
}
